<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active: index === activeIndex}"
        @click="changeRole(index,item)"
      >
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>

        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/addRole?id=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delBtn(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="功能权限" name="perms">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="trees"
                :data="[...item]"
                :props="defaultProps"
                show-checkbox
                node-key="id"
                default-expand-all
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="user">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>

        </el-tab-pane>
      </el-tabs>

    </div>
  </div></template>

<script>
import { delRole, getFunction, getRoleDetail, getRoles, getRoleUser } from '@/api/role'
import SvgIcon from '@/components/SvgIcon'
import { treeToDisabled } from '@/api/treeToDisabled'
export default {
  name: 'Role',
  components: {
    SvgIcon
  },
  data() {
    return {
      activeName: 'perms',
      roleList: [],
      treeList: [],
      activeIndex: 0,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      params: {
        page: 1,
        pageSize: 10
      },
      currentId: null,
      roleUserList: []
    }
  },
  async created() {
    await this.getRoleList()
    await this.getFunctionList()
    this.$nextTick(() => {
      this.getDetail()
    })
  },
  methods: {
    async getRoleList() {
      const res = await getRoles()
      this.roleList = res.data
    },
    // 切换角色
    changeRole(idx, item) {
      this.activeIndex = idx
      this.currentId = item.roleId
      this.getDetail(this.currentId)
      this.getRoleUserList()
    },
    // 获取功能列表
    async getFunctionList() {
      const { data } = await getFunction()
      this.treeList = treeToDisabled(data)
      // 给功能列表每一项添加disabled禁用
    },
    // 获取角色详情,并且给树形组件添加默认选中
    async getDetail(value) {
      this.currentId = value || this.roleList[0].roleId
      const { data: { perms }} = await getRoleDetail(this.currentId)
      const trees = this.$refs.trees
      // 给树形组件添加默认选中
      trees.forEach((item, index) => {
        item.setCheckedKeys(perms[index])
      })
    },
    // 切换标签页
    handleClick() {
      console.log(this.activeName)
      if (this.activeName === 'user') {
        this.getRoleUserList()
      } else {
        this.getDetail(this.currentId)
      }
    },
    async getRoleUserList() {
      const { data: { rows }} = await getRoleUser({ id: this.currentId, ...this.params })
      this.roleUserList = rows
    },
    // 删除功能
    delBtn(id) {
      this.$confirm('是否确认删除当前角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
